package com.mxsat.system.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mxsat.common.enums.dict.DataStatusEnum;
import com.mxsat.system.dao.SysDictDao;
import com.mxsat.system.dao.SysDictItemDao;
import com.mxsat.system.entity.SysDict;
import com.mxsat.system.entity.SysDictItem;
import com.mxsat.system.service.SysDictionaryService;

@Service
public class SysDictionaryServiceImpl implements SysDictionaryService{
 
	@Autowired
	private SysDictDao sysDictDao;
	
	@Autowired
	private SysDictItemDao sysDictItemDao;
	
	@Override
	public IPage<SysDict> findSysDictByPage(IPage<SysDict> page) throws Exception {
		return sysDictDao.selectDictByPage(page, new HashMap<>());
	}

	@Override
	public IPage<SysDictItem> findSysDictItemByPage(IPage<SysDictItem> page,String code) throws Exception {
		Map<String, Object> params = new HashMap<>();
		params.put("code", code);
		return sysDictItemDao.selectDictItemByPage(page, params);
	}

	@Override
	public boolean addSysDictItem(SysDictItem item) throws Exception {
		item.setData_status(DataStatusEnum.正常.toCode());
		return sysDictItemDao.save(item);
	}

	@Override
	public boolean modfiySysDict(SysDict dict) throws Exception {
		return sysDictDao.updateById(dict);
	}
	
	@Transactional
	@Override
	public Boolean removeSysDict(String code) throws Exception {
		this.removeSysDictItems(code);
		return sysDictDao.remove(new QueryWrapper<SysDict>().eq("code", code));
	}
	
	@Override
	public boolean removeSysDictItems(String code) throws Exception {
		return sysDictItemDao.remove(new QueryWrapper<SysDictItem>().eq("code", code));
	}
	
	@Override
	public boolean modifySysDictItem(SysDictItem item) throws Exception {
		return sysDictItemDao.updateById(item);
	}

	@Override
	public boolean addSysDict(SysDict dict) throws Exception {
		dict.setData_status(DataStatusEnum.正常.toCode());
		return sysDictDao.save(dict);
	}

	@Override
	public boolean removeSysDictItem(Long id) throws Exception {
		return sysDictItemDao.removeById(id);
	}

}
